<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="医生" prop="rDoctor">
        <el-input
          v-model="queryParams.rDoctor"
          placeholder="请输入医生"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="科室" prop="tDepar">
        <el-input
          v-model="queryParams.tDepar"
          placeholder="请输入科室"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="患者姓名" prop="pName">
        <el-input
          v-model="queryParams.pName"
          placeholder="请输入患者姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="就诊状态" prop="tStatus">
        <el-input
          v-model="queryParams.tStatus"
          placeholder="1未就诊2已就诊0退号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button @click="addRegsient"  style="float:right" type="primary">
                <i class="el-icon-circle-plus-outline"></i>
                新增挂号</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['patientRegster:regsterList:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['patientRegster:regsterList:edit']"
        >修改</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['patientRegster:regsterList:remove']"
        >删除</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['patientRegster:regsterList:export']"
        >导出</el-button>
      </el-col>
      
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="regsterListList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="id" width="55" align="center" />
      <el-table-column label="挂号单号" align="center" prop="rNo" />
      <el-table-column label="患者姓名" align="center" prop="pName" width="180">
        
      </el-table-column>
      <el-table-column label="性别" align="center" prop="pSex" />
      <el-table-column label="年龄" align="center" prop="pAge" />
      <el-table-column label="手机号码" align="center" prop="pPhone" />
      <el-table-column label="科室" align="center" prop="tDepar" />
      <el-table-column label="接诊医生" align="center" prop="rDoctor" />
      <el-table-column label="接诊时间" align="center" prop="rstartDate" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rStartDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应收金额" align="center" prop="cReceivable" />
      <el-table-column label="实收金额" align="center" prop="cRealityMoney" />
      <el-table-column label="就诊状态" align="center" prop="tStatus" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_patient_tstatus" :value="scope.row.tStatus"/>
        </template>
      </el-table-column>
      
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['patientRegster:regsterList:edit']"
          >编辑</el-button>

         
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改挂号管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="主键" prop="id">
          <el-input v-model="form.id" placeholder="请输入主键" />
        </el-form-item>
        <el-form-item label="挂号单号" prop="rNo">
          <el-input v-model="form.rNo" placeholder="请输入挂号单号" />
        </el-form-item>
        <el-form-item label="挂号时间" prop="rStartDate">
          <el-date-picker clearable
            v-model="form.rStartDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择挂号时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="医生" prop="rDoctor">
          <el-input v-model="form.rDoctor" placeholder="请输入医生" />
        </el-form-item>
        <el-form-item label="挂号费" prop="rRegister">
          <el-input v-model="form.rRegister" placeholder="请输入挂号费" />
        </el-form-item>
        <el-form-item label="诊疗费" prop="rDisease">
          <el-input v-model="form.rDisease" placeholder="请输入诊疗费" />
        </el-form-item>
        <el-form-item label="挂号员" prop="rRegisterClerk">
          <el-input v-model="form.rRegisterClerk" placeholder="请输入挂号员" />
        </el-form-item>
        <el-form-item label="科室" prop="tDepar">
          <el-input v-model="form.tDepar" placeholder="请输入科室" />
        </el-form-item>
        <el-form-item label="状态" prop="tDepar">
          <el-input v-model="form.tStatus" placeholder="请输入科室" />
        </el-form-item>

        <el-form-item label="患者id" prop="patientId">
          <el-input v-model="form.patientId" placeholder="请输入患者id" />
        </el-form-item>
      
       
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRegsterList, getRegsterList, delRegsterList, addRegsterList, updateRegsterList } from "@/api/patientRegster/regsterList";

export default {
  name: "RegsterList",
  dicts: ['sys_user_sex','sys_patient_tstatus'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedPatient: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 挂号管理表格数据
      regsterListList: [],
      // 挂号管理表格数据
      patientList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        rDoctor: null,
        tDepar: null,
        pName:null,
        tStatus:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询挂号管理列表 */
    getList() {
      this.loading = true;
      listRegsterList(this.queryParams).then(response => {
        this.regsterListList = response.rows;
        console.log(response.rows);
        this.total = response.total;
        this.loading = false;
      });
    },
    addRegsient(){
      this.$router.push({
        path: 'regster3',
       
      })
        },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        rNo: null,
        rStartDate: null,
        rDoctor: null,
        rRegister: null,
        rDisease: null,
        rRegisterClerk: null,
        tType: null,
        tDepar: null,
        tStatus: null,
        patientId: null
      };
      this.patientList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加挂号管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row);
      this.reset();
      const id = row.id || this.ids
      getRegsterList(id).then(response => {
        this.form = response.data;
        this.patientList = response.data.patientList;
        this.open = true;
        this.title = "修改挂号管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.patientList = this.patientList;
          if (this.form.id != null) {
            updateRegsterList(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRegsterList(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除挂号管理编号为"' + ids + '"的数据项？').then(function() {
        return delRegsterList(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 挂号管理序号 */
    rowPatientIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 挂号管理添加按钮操作 */
    handleAddPatient() {
      let obj = {};
      obj.pName = "";
      obj.pNo = "";
      obj.pAge = "";
      obj.pStartDate = "";
      obj.pSex = "";
      obj.pPhone = "";
      obj.pCard = "";
      obj.pProvince = "";
      obj.pPlace = "";
      obj.pArea = "";
      obj.createTime = "";
      this.patientList.push(obj);
    },
    /** 挂号管理删除按钮操作 */
    handleDeletePatient() {
      if (this.checkedPatient.length == 0) {
        this.$modal.msgError("请先选择要删除的挂号管理数据");
      } else {
        const patientList = this.patientList;
        const checkedPatient = this.checkedPatient;
        this.patientList = patientList.filter(function(item) {
          return checkedPatient.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handlePatientSelectionChange(selection) {
      this.checkedPatient = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('patientRegster/regsterList/export', {
        ...this.queryParams
      }, `regsterList_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
